package chapter03;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;


@WebServlet("/servlet")
public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet.........get.........");
        String parameterString = req.getQueryString();
        System.out.println(parameterString);
        String[] parametersArr = parameterString.split("&");
        String username = parametersArr[0].split("=")[1];
        String password = parametersArr[1].split("=")[1];

        System.out.println(username);
        System.out.println(password);

        if ("admin".equals(username)&&"admin".equals(password)) {
            System.out.println("登录成功！");
        }else{
            System.out.println("登录失败！");
        }
        // for (int i = 0; i <parametersArr.length ; i++) {
        //     System.out.println(Arrays.toString(parametersArr));
        //     System.out.println(parametersArr[i]);
        //     String[] parameterArr = parameterString.split("=");
        //     for (int j = 0; j <parameterArr.length ; j++) {
        //         System.out.println(parameterArr[j]);

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet.........post........");
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine())!= null){
            System.out.println(line);
            String[] parametersArr = line.split("&");
            String username = parametersArr[0].split("=")[1];
            String password = parametersArr[1].split("=")[1];

            if ("admin".equals(username)&&"admin".equals(password)) {
                System.out.println("登录成功！");
            }else{
                System.out.println("登录失败！");
            }
        }
    }
}
